package net.hashcoding.cookbook.core;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Administrator on 2017/6/24.
 */
public abstract class BaseServlet extends HttpServlet {

    @Override
    public void init() {
        initialize();
    }

    protected abstract void initialize();

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) {
        dispatch(HttpMethod.GET, request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) {
        dispatch(HttpMethod.POST, request, response);
    }

    @Override
    public void doPut(HttpServletRequest request, HttpServletResponse response) {

        dispatch(HttpMethod.PUT, request, response);
    }

    @Override
    public void doDelete(HttpServletRequest request, HttpServletResponse response) {
        dispatch(HttpMethod.DELETE, request, response);
    }

    @Override
    public void doHead(HttpServletRequest request, HttpServletResponse response) {
        dispatch(HttpMethod.HEAD, request, response);
    }

    @Override
    public void doTrace(HttpServletRequest request, HttpServletResponse response) {
        dispatch(HttpMethod.TRACE, request, response);
    }

    @Override
    public void doOptions(HttpServletRequest request, HttpServletResponse response) {
        dispatch(HttpMethod.OPTIONS, request, response);
    }

    protected abstract void dispatch(HttpMethod HttpMethod,
                                     HttpServletRequest request, HttpServletResponse response);
}
